package dialogos;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.NumberFormatter;

import accesoADatos.AccesoADatos;

import datos.Caja;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class CierreCajaDialog extends javax.swing.JDialog {
	
	private JLabel lFecha;
	private JFormattedTextField tSaldoEfectivo;
	private JButton bCancelar;
	private JButton bAceptar;
	private JTextField tDiferencia;
	private JLabel lDiferencia;
	private JTextField tTotalRetiros;
	private JTextField tTotalFacturado;
	private JTextField tSaldoInicial;
	private JLabel lTotalRetiros;
	private JLabel lTotalFacturado;
	private JLabel lSaldoEfectivo;
	private JLabel lSaldoInicial;
	public SimpleDateFormat formateador = new SimpleDateFormat("dd ' de ' MMMM 'de ' yyyy'  -  'HH':'mm 'Hs.'", new Locale("Es",""));
	private Caja c = null;

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}


	/**
	* Auto-generated main method to display this JDialog
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				JFrame frame = new JFrame();
				CierreCajaDialog inst = new CierreCajaDialog(frame);
				inst.setVisible(true);
			}
		});
	}
	
	public CierreCajaDialog(JFrame frame) {
		super(frame,"Cierre de caja",true);
		initGUI();
	}
	
	private void initGUI() {
		try {
			FlowLayout thisLayout = new FlowLayout();
			thisLayout.setVgap(10);
			thisLayout.setHgap(10);
			getContentPane().setLayout(thisLayout);
			c = AccesoADatos.getInstance().getCaja();
			{
				lFecha = new JLabel();
				getContentPane().add(lFecha);
				lFecha.setText("Fecha:  " + this.formateador.format(c.getFecha()));
				lFecha.setPreferredSize(new java.awt.Dimension(260, 20));
				lFecha.setHorizontalAlignment(SwingConstants.CENTER);
				lFecha.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
				lFecha.setForeground(new java.awt.Color(0,0,0));
			}
			{
				lSaldoInicial = new JLabel();
				getContentPane().add(lSaldoInicial);
				lSaldoInicial.setText("Saldo Inicial");
				lSaldoInicial.setPreferredSize(new java.awt.Dimension(140, 20));
				lSaldoInicial.setHorizontalAlignment(SwingConstants.CENTER);
				lSaldoInicial.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
				lSaldoInicial.setForeground(new java.awt.Color(0,0,0));
			}
			{
				tSaldoInicial = new JTextField();
				tSaldoInicial.setEditable(false);
				tSaldoInicial.setText(c.getSaldoInicial().toString());
				getContentPane().add(tSaldoInicial);
				tSaldoInicial.setPreferredSize(new java.awt.Dimension(110, 20));
				tSaldoInicial.setFont(new java.awt.Font("Dialog",1,12));
				tSaldoInicial.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
				tSaldoInicial.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			{
				lTotalFacturado = new JLabel();
				getContentPane().add(lTotalFacturado);
				lTotalFacturado.setText("Total Facturado");
				lTotalFacturado.setPreferredSize(new java.awt.Dimension(140, 20));
				lTotalFacturado.setHorizontalAlignment(SwingConstants.CENTER);
				lTotalFacturado.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
				lTotalFacturado.setForeground(new java.awt.Color(0,0,0));
			}
			{
				tTotalFacturado = new JTextField();
				getContentPane().add(tTotalFacturado);
				Double total=AccesoADatos.getInstance().getTotalFacturado(c.getFecha(), new Date())+
				AccesoADatos.getInstance().getTotalVentas(c.getFecha(), new Date());
				tTotalFacturado.setText(total.toString());
				tTotalFacturado.setPreferredSize(new java.awt.Dimension(110, 20));
				tTotalFacturado.setEditable(false);
				tTotalFacturado.setFont(new java.awt.Font("Dialog",1,12));
				tTotalFacturado.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
				tTotalFacturado.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			{
				lTotalRetiros = new JLabel();
				getContentPane().add(lTotalRetiros);
				lTotalRetiros.setText("Total Retiros");
				lTotalRetiros.setPreferredSize(new java.awt.Dimension(140, 20));
				lTotalRetiros.setHorizontalAlignment(SwingConstants.CENTER);
				lTotalRetiros.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
				lTotalRetiros.setForeground(new java.awt.Color(0,0,0));
			}
			{
				tTotalRetiros = new JTextField();
				tTotalRetiros.setText(String.valueOf(AccesoADatos.getInstance().getTotalRetiros(c)+AccesoADatos.getInstance().getTotalCompras(c, new Date())));
				getContentPane().add(tTotalRetiros);
				tTotalRetiros.setPreferredSize(new java.awt.Dimension(110, 20));
				tTotalRetiros.setEditable(false);
				tTotalRetiros.setFont(new java.awt.Font("Dialog",1,12));
				tTotalRetiros.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
				tTotalRetiros.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			{
				lSaldoEfectivo = new JLabel();
				getContentPane().add(lSaldoEfectivo);
				lSaldoEfectivo.setText("Saldo Efectivo");
				lSaldoEfectivo.setPreferredSize(new java.awt.Dimension(140, 20));
				lSaldoEfectivo.setHorizontalAlignment(SwingConstants.CENTER);
				lSaldoEfectivo.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
				lSaldoEfectivo.setForeground(new java.awt.Color(0,0,0));
			}
			{
				tSaldoEfectivo = new JFormattedTextField();
				NumberFormat vFormat = NumberFormat.getCurrencyInstance();
				tSaldoEfectivo.setValue(new Double(0));
				NumberFormat eFormat = NumberFormat.getNumberInstance(Locale.ENGLISH);
				eFormat.setGroupingUsed(false);
				NumberFormatter dvFormat = new NumberFormatter(vFormat);
				NumberFormatter deFormat = new NumberFormatter(eFormat);
				DefaultFormatterFactory fact = new DefaultFormatterFactory(dvFormat,dvFormat,deFormat);
				tSaldoEfectivo.setFormatterFactory(fact);
				tSaldoEfectivo.setPreferredSize(new java.awt.Dimension(110, 20));
				tSaldoEfectivo.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
				tSaldoEfectivo.setHorizontalAlignment(SwingConstants.RIGHT);
				this.add(tSaldoEfectivo);
				tSaldoEfectivo.addKeyListener(new KeyAdapter() {
					public void keyPressed(KeyEvent evt) {
						if(evt.getKeyCode() == KeyEvent.VK_ENTER){
							CierreCajaDialog.this.tSaldoEfectivo.setFocusable(false);
							CierreCajaDialog.this.tSaldoEfectivo.setFocusable(true);
						}
					}
				});
				tSaldoEfectivo.addFocusListener(new FocusAdapter() {
					public void focusLost(FocusEvent evt) {
						double saldoinicial = Double.valueOf(tSaldoInicial.getText()).doubleValue();
						double efectivo = (Double.valueOf(tSaldoEfectivo.getText())).doubleValue();
						double facturado = Double.valueOf(tTotalFacturado.getText()).doubleValue();
						double retiros = Double.valueOf(tTotalRetiros.getText()).doubleValue();	
						tDiferencia.setText(new Double(-saldoinicial-facturado+retiros+efectivo).toString());
					}
				});
			}
			{
				lDiferencia = new JLabel();
				getContentPane().add(lDiferencia);
				lDiferencia.setText("Diferencia");
				lDiferencia.setHorizontalAlignment(SwingConstants.CENTER);
				lDiferencia.setPreferredSize(new java.awt.Dimension(140, 20));
				lDiferencia.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
				lDiferencia.setForeground(new java.awt.Color(0,0,0));
			}
			{
				tDiferencia = new JTextField();
				getContentPane().add(tDiferencia);
				tDiferencia.setPreferredSize(new java.awt.Dimension(110, 20));
				tDiferencia.setEditable(false);
				tDiferencia.setFont(new java.awt.Font("Dialog",1,12));
				tDiferencia.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
				tDiferencia.setHorizontalAlignment(SwingConstants.RIGHT);
			}
			{
				bAceptar = new JButton();
				getContentPane().add(bAceptar);
				bAceptar.setText("Aceptar");
				bAceptar.setPreferredSize(new java.awt.Dimension(100, 25));
				bAceptar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						double efectivo = Double.valueOf(tSaldoEfectivo.getValue().toString());
						double facturado = Double.valueOf(tTotalFacturado.getText()).doubleValue();
						c.setSaldoEfectivo(efectivo);
						c.setTotalFacturado(facturado);
						AccesoADatos.getInstance().setObjeto(c);
						CierreCajaDialog.this.dispose();
					}
				});
			}
			{
				bCancelar = new JButton();
				getContentPane().add(bCancelar);
				bCancelar.setText("Cancelar");
				bCancelar.setPreferredSize(new java.awt.Dimension(100, 25));
				bCancelar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						CierreCajaDialog.this.dispose();
					}
				});
			}
			pack();
			this.setSize(305, 280);
			this.setPreferredSize(new java.awt.Dimension(305, 280));
			this.setLocationRelativeTo(null);
			this.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
